package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.TravelLike;
import cn.wolfcode.trip.base.mapper.TravelLikeMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelLikeServiceImpl implements ITravelLikeService {
    @Autowired
    private TravelLikeMapper mapper;
    @Autowired
    private TravelMapper travelMapper;

    @Override
    public void delete(TravelLike travelLike) {
        mapper.deleteByInfo(travelLike);
        travelMapper.updateLikeCount(travelLike.getTravelId(),-1);
    }

    @Override
    public void like(TravelLike travelLike) {
        travelLike.setCreateTime(new Date());
        mapper.insert(travelLike);
        travelMapper.updateLikeCount(travelLike.getTravelId(),1);

    }

    @Override
    public TravelLike getLike(TravelLike travelLike) {
        return mapper.selectByInfo(travelLike);
    }

    @Override
    public Integer getLikeCount(Long travelId) {
       return travelMapper.getLikeCount(travelId);
    }

    @Override
    public int getTravelLikeNum(Long id, boolean isRead) {
        return mapper.selectTravelLikeNum(id,isRead);
    }

    @Override
    public void changeLikeIsReadById(Long id, boolean b) {
        List<Long> travelIds=mapper.selectTravelIdByAuthorId(id);
        if (travelIds.size()>0){
            mapper.updateLikeIsRead(travelIds,b);
        }
    }

    @Override
    public PageInfo<TravelLike> getTravelLikeIMes(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<TravelLike> list = mapper.selectTravelLikeInfo(qo);
        return new PageInfo<>(list);
    }
}
